import time

from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

#中间件，相当于切面编程
@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    time.sleep(1)
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers["X-Process-Time-Yinyu"] = str(process_time)
    return response


#请求示例
@app.get("/test/")
async def read_item():
    return "OK"


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8000)